﻿/**
 * User only
 */
Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', baseUrl + '/extjs/ux');
Ext.define('Cps.view.dashboard.Dashboard1', {
	extend : 'Ext.container.Viewport',
	alias : 'widget.dashboard1',
	layout: 'border',
	requires: [
		'Ext.chart.*',
		'Ext.ux.grid.feature.MultiGroupingSummary',
		'Ext.ux.grid.plugin.GroupingPanel',
		'Ext.ux.grid.Printer1'
	],
	id: 'dashboard1',
    initComponent: function() {
			Ext.util.Format.thousandSeparator = "'";
			Ext.util.Format.decimalSeparator = '.'
    	this.items = [
	    		{
					region: 'north',
					title: 'Concept Web',
					header: {
						cls: 'header',
						id: 'headerNorth',
						titleAlign: 'center',
						titlePosition: 0,
						items: [
							{
								xtype: 'button',
								margin: '0 20px',
								cls: 'home',
								text: Cps.Locale.Dashboard1.home_btn3 + ': ' + localStorage.userName,
								menu: [
									{
										text: Cps.Locale.Dashboard1.logout,
										iconCls : 'exit',
										handler: function(event, toolEl, panelHeader) {
											location.href = baseUrl + '/user/logout';
										}
									}
								]
							},
							{
								xtype: 'button',
								id: 'language',
								cls: 'home',
								text: Cps.Locale.Dashboard1.home_btn2 + ': ' + (CheckLang() == "fr" ? '<div class="flag flag-fr" style="display: inline-block;"></div>' : '<div class="flag flag-gb" style="display: inline-block;"></div>'),
								menu: [
									{
										iconCls : 'flag flag-gb',
										id: 'flagEn',
										text:'English',
										handler: function () {
											changeLanguage('en');
										},
										listeners: {
											afterrender: {
												fn: function() {
													if (CheckLang() == "fr") 
														Ext.getCmp('flagFr').hide();
												}
											}
										}
									},
									{
										iconCls : 'flag flag-fr',
										id: 'flagFr',
										text:'France',
										handler: function () {
											changeLanguage('fr');
										},
										listeners: {
											afterrender: {
												fn: function() {
													if (CheckLang() == "en") 
														Ext.getCmp('flagEn').hide();
												}
											}
										}
									}
								]
							}							
						]
					}
	    		},
				{
					xtype: 'panel',
					region: 'west',
					collapsible: true,
					split: true,
					width: 180,
					title: Cps.Locale.Dashboard1.title_west,
					layout: 'fit',
					items: [
						{
							xtype: 'panel',
							layout: 'fit',
							items: [
								{
									xtype: 'treepanel',
									id: 'trp',
									itemId: 'trpItem',
									border: false,
									lines: false,
									rootVisible: false,
									root: {
										expanded: true,
										children: [
											{ text: Cps.Locale.Dashboard1.node1, leaf: true, iconCls: 'node1'},
											{ text: Cps.Locale.Dashboard1.node2, expanded: true, children: [
												{ text: Cps.Locale.Dashboard1.node2_1, leaf: true, iconCls: 'node2_1'},
												{ text: Cps.Locale.Dashboard1.node2_2, leaf: true, iconCls: 'node2_2'},
												{ text: Cps.Locale.Dashboard1.node2_3, leaf: true, iconCls: 'node2_3'}
											]}
										]
									},
									listeners: {
										itemclick: function (treepanel, record, item, index, e, eOpts) {
											sessionStorage.tree = index;
											onDisplay(2);
										}
									}
							}]							
						}
					]
	    		},
				{
					xtype: 'panel',
					border: false,
					region: 'center',
					layout: { type: 'vbox', align: 'stretch'},
					items: [
						{
							xtype: 'panel',
							id: 'pnFilter',
							autoHeight: true,
							bodyPadding: 5,
							title: Cps.Locale.Dashboard1.title_center1,
							tools: [
								{
									xtype: 'combo',
									id: 'cbb',
									width: 200,
									editable: false,
									store: Ext.create('Ext.data.Store', {
										fields: ['id', 'name'],
										data: [
											{"id":"1", "name":Cps.Locale.Dashboard1.combo1},
											{"id":"2", "name":Cps.Locale.Dashboard1.combo2},
											{"id":"3", "name":Cps.Locale.Dashboard1.combo3},
											{"id":"4", "name":Cps.Locale.Dashboard1.combo4},
											{"id":"5", "name":Cps.Locale.Dashboard1.all}
										],
										}),
									queryMode: 'local',
									displayField: 'name',
									valueField: 'id',
									value: '5',
									listeners: {
										select: function(combo, records, eOpts) {
											
											onDisplay(2);
										}
									}
								},
								{
									xtype: 'button',
									iconCls: 'btnFilter1',
									text: Cps.Locale.Dashboard1.btnfilter1,
									action: 'criteria'
								}
							]
						},
						{
							xtype: 'panel',
							id: 'zone4',
							flex: 6,
							layout: 'fit',
							title: Cps.Locale.Dashboard1.title_center2,
							tools: [	
								{
									xtype: 'combo',
									id: 'cbb1',
									editable: false,
									hidden: true,
									store: Ext.create('Ext.data.Store', {
										fields: ['id', 'name'],
										data: [
											{"id":"1", "name":Cps.Locale.Dashboard1.chart1},
											{"id":"2", "name":Cps.Locale.Dashboard1.chart2},
											{"id":"3", "name":Cps.Locale.Dashboard1.chart3},
											{"id":"4", "name":Cps.Locale.Dashboard1.chart4},
											{"id":"5", "name":Cps.Locale.Dashboard1.chart5},
											{"id":"6", "name":Cps.Locale.Dashboard1.chart6},
											{"id":"7", "name":Cps.Locale.Dashboard1.chart7},
											{"id":"8", "name":Cps.Locale.Dashboard1.chart8}
											//{"id":"9", "name":Cps.Locale.Dashboard1.chart9}
										],
										}),
									queryMode: 'local',
									displayField: 'name',
									valueField: 'id',
									value: "1",
									listeners: {
										select: function() {
											onDisplay(2);
										}
									}
								},
								{
									xtype: 'button',
									tooltip: Cps.Locale.Dashboard1.refresh,
									iconCls: 'btnZone4_1',
									cls: 'toolZone4',
									handler: function() {
										var zone4 = Ext.getCmp('zone4');
										if (zone4.items.items[0].isVisible())
										{
											onDisplay(3);
										}
									}
								},
								{
									xtype: 'button',
									tooltip: Cps.Locale.Dashboard1.preview,
									iconCls: 'btnZone4_2',
									cls: 'toolZone4',
									handler : function(){
										// var grid = Ext.getCmp('zone4').items.items[0];
										// Ext.ux.grid.Printer1.printAutomatically = false;
										// Ext.ux.grid.Printer1.print(grid);
										var zone4= Ext.getCmp('zone4'), grid;
										if (zone4.items.items.length > 0)
										{
											grid = zone4.items.items[0];
											if (grid.$className == 'Ext.grid.Panel'){
												grid.print({
													printCSS: [
														baseUrl + '/extjs/resources/css/ext-all.css',
														baseUrl + '/extjs/ux/grid/feature/resources/MultiGroupingSummary.css',
														baseUrl + '/extjs/ux/grid/plugin/resources/GroupingPanel.css'
													],
													preview: true,
													printStyle: true,
													printTitle: 'List',
													printWidth:11,
													printHeight:8.5
												});
											}
										}
									}
								},
								{
									xtype: 'button',
									tooltip: Cps.Locale.Dashboard1.print,
									iconCls: 'btnZone4_3',
									cls: 'toolZone4',
									handler: function(button){
										var zone4= Ext.getCmp('zone4'), grid;
										if (zone4.items.items.length > 0)
										{
											grid = zone4.items.items[0];
											if (grid.$className == 'Ext.grid.Panel'){
												grid.print({
													printCSS: [
														baseUrl + '/extjs/resources/css/ext-all.css',
														baseUrl + '/extjs/ux/grid/feature/resources/MultiGroupingSummary.css',
														baseUrl + '/extjs/ux/grid/plugin/resources/GroupingPanel.css'
													],
													printStyle: true,
													preview: false,
													printTitle: Cps.Locale.Dashboard1.preview,
													printWidth:11,
													printHeight:8.5
												});
											}
										}
									}
								},
								{
									xtype: 'button',
									tooltip: Cps.Locale.Dashboard1.exportToExcel,
									iconCls: 'btnZone4_4',
									cls: 'toolZone4',
									handler : function(button, event){
										var zone4= Ext.getCmp('zone4'), grid;
										if (zone4.items.items.length > 0)
										{
											grid = zone4.items.items[0];
											if (grid.$className == 'Ext.grid.Panel'){
												grid.downloadExcelXml();
											}
										}
									}
								},
							]
						}
					]
	    		}
	    ];
    	this.listeners = {
			afterrender: function() {
				Ext.Ajax.request({
					method: 'GET',
					url: Cps.Config.Api.getCenter,
					success: function(data) {
						data = Ext.JSON.decode(data.responseText).data;
						data = data ? data.ttCenter : null;
						data = data ? data.ttCenterRow : null;
						var menu = Ext.create('Ext.menu.Menu', { plain: true}), hid = sessionStorage.centre == undefined || sessionStorage.centre == null || sessionStorage.centre == '' ? 0 : sessionStorage.centre;
						for(i = 0; i < data.length; i++) {
							menu.add({ text: data[i].CenterCode + " " + data[i].CenterName, value: data[i].CenterId, iconCls: 'unchecked', hidden: i == hid ? true : false,
										handler: function () {
											var items = this.ownerCt.items.items, pnFilter = Ext.getCmp('pnFilter');;
											for (var i = 0; i < items.length; i++)
											{
												if (items[i] != this)
												{
													//items[i].iconEl.addCls('unchecked');
													//items[i].iconEl.removeCls('checked');
													items[i].show();
												}
												else
												{
													//items[i].iconEl.removeCls('unchecked');
													sessionStorage.centre = i;
												}
											}
											//this.iconEl.addCls('checked');
											this.hide();
											sessionStorage.removeItem('showForm');
											sessionStorage.removeItem('filter');
											pnFilter.update('');
											Ext.getCmp('centerManager').setText(Cps.Locale.Dashboard1.home_btn1 + ': ' + this.text);
										}
							});
						}
						//menu.items.items[0].iconCls = 'checked';
						var headerNorth = Ext.getCmp('headerNorth'), btnCentre = Ext.create('Ext.button.Button', {
							cls: 'home',
							text: Cps.Locale.Dashboard1.home_btn1 + ': ' + menu.items.items[hid].text,
							id: 'centerManager',
							menu: menu
						});
						headerNorth.insert(0, btnCentre);
						if (sessionStorage.tree != undefined && sessionStorage.tree != null && sessionStorage.tree != '')
							Ext.getCmp('trp').getSelectionModel().select(parseInt(sessionStorage.tree));
						onDisplay(2);
					},
					scope: this
				});
			},
			afterlayout: function() {
				var cbb = Ext.getCmp('cbb'), width = 0;
				if (cbb.ownerCt.items.items[0].text != undefined)
					width = textWidth(cbb.ownerCt.items.items[0].text) + 20;
				else
					width = textWidth(cbb.ownerCt.items.items[0].renderData.title) + 20;
				cbb.el.setStyle('left',width + 'px');
			}
		},
    	this.callParent(arguments);
    }
});
function CheckLang() {
	var lang;
	var scripts = document.getElementsByTagName('script');
	for (var i = 0; i < scripts.length; i++)
	{
		var c = scripts[i].src.search('locale');
		if (c != -1)
		{
			var b = scripts[i].src;
			lang = b.substring(b.search('locale') + 7 ,b.length).split('.')[0];
			break;
		}
	}
	return lang;
};
